package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Joiner;
import org.apache.commons.collections.CollectionUtils;
import org.example.dto.AddSpuDto;
import org.example.dto.EditSpuDto;
import org.example.entity.SysSpu;
import org.example.entity.SysSubCategory;
import org.example.mapper.*;
import org.example.model.PageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class SysSpuService {

    @Resource
    private SysSpuMapper sysSpuMapper;

    @Resource
    private SpuImgMapper spuImgMapper;

    @Resource
    private SpuKeyMapper spuKeyMapper;

    @Resource
    private SysSubCategoryMapper sysSubCategoryMapper;

    //渲染数据
    public PageBean<SysSpu> getAll(Integer page, Integer size) {
        Page<SysSpu> userPage = PageHelper.startPage(page, size);
        sysSpuMapper.selectList(null);
        return PageBean.<SysSpu>builder()
                .data(userPage.getResult())
                .total(userPage.getTotal())
                .current(userPage.getPageNum())  // 当前页
                .build();
    }

    //删除数据
    public void deleteById(Integer id){
        sysSpuMapper.deleteById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",id);
        spuKeyMapper.delete(wrapper);
        spuImgMapper.delete(wrapper);
    }

    //添加数据
    public void addSpu(AddSpuDto addSpuDto){
        SysSpu sysSpu = SysSpu.builder()
                .createTime(new Date())
                .categoryId(addSpuDto.getCategoryId())
                .description(addSpuDto.getDescription())
                .img(addSpuDto.getMainImgUrl())
                .online(addSpuDto.getOnline())
                .price(addSpuDto.getPrice())
                .discountPrice(addSpuDto.getDiscountPrice())
                .title(addSpuDto.getTitle())
                .subtitle(addSpuDto.getSubtitle())
                .updateTime(new Date())
                .tags(Joiner.on("#").join(addSpuDto.getDynamicTags())).build();
        sysSpuMapper.insert(sysSpu);
        Integer spuId = sysSpu.getId();
        List<String> imgList = addSpuDto.getCarouselImages();
        List<Integer> specKeyIds = addSpuDto.getSpecKeysIds();
        if(CollectionUtils.isNotEmpty(imgList)){
            spuImgMapper.batchInsertBySpuId(spuId,imgList);
        }
        if(CollectionUtils.isNotEmpty(specKeyIds)){
            spuKeyMapper.batchInsertBySpuId(spuId,specKeyIds);
        }

    }

    //获取id回显
    public EditSpuDto getById(Integer id){
        SysSpu sysSpu = sysSpuMapper.selectById(id);
        SysSubCategory category = sysSubCategoryMapper.selectById(sysSpu.getCategoryId());
        List<Integer> specKeysIds = spuKeyMapper.getBySpuId(id);
        List<String> carouselImages = spuImgMapper.getBySpuId(id);
        List<String> dynamicTags = Arrays.asList(sysSpu.getTags().split("#"));
        EditSpuDto editSpuDto = EditSpuDto.builder()
                .category(category)
                .id(sysSpu.getId())
                .online(sysSpu.getOnline())
                .mainImgUrl(sysSpu.getImg())
                .description(sysSpu.getDescription())
                .price(sysSpu.getPrice())
                .title(sysSpu.getTitle())
                .subtitle(sysSpu.getSubtitle())
                .discountPrice(sysSpu.getDiscountPrice())
                .specKeysIds(specKeysIds)
                .carouselImages(carouselImages)
                .dynamicTags(dynamicTags)
                .build();
        return editSpuDto;
    }

    //编辑数据
    public void editSpu(EditSpuDto editSpuDto){
        Integer spuId = editSpuDto.getId();
        List<Integer> specKeyIds = editSpuDto.getSpecKeysIds();
        List<String> imgList = editSpuDto.getCarouselImages();
        Integer categoryId = 0;
        if(editSpuDto.getCategory() != null){
            categoryId = editSpuDto.getCategory().getId();
        }
        SysSpu sysSpu = SysSpu.builder()
                .id(spuId)
                .tags(Joiner.on("#").join(editSpuDto.getDynamicTags()))
                .updateTime(new Date())
                .subtitle(editSpuDto.getSubtitle())
                .title(editSpuDto.getTitle())
                .price(editSpuDto.getPrice())
                .discountPrice(editSpuDto.getDiscountPrice())
                .online(editSpuDto.getOnline())
                .categoryId(categoryId)
                .img(editSpuDto.getMainImgUrl())
                .description(editSpuDto.getDescription())
                .build();
        sysSpuMapper.updateById(sysSpu);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuId);
        spuKeyMapper.delete(wrapper);
        spuImgMapper.delete(wrapper);
        if (CollectionUtils.isNotEmpty(specKeyIds)){
            spuKeyMapper.batchInsertBySpuId(spuId,specKeyIds);
        }
        if (CollectionUtils.isNotEmpty(imgList)){
            spuImgMapper.batchInsertBySpuId(spuId,imgList);
        }
    }
}
